package io.scnb.coupon.calculation.controller;

import com.alibaba.fastjson.JSON;
import io.scnb.coupon.calculation.api.beans.ShoppingCart;
import io.scnb.coupon.calculation.api.beans.SimulationOrder;
import io.scnb.coupon.calculation.api.beans.SimulationResponse;
import io.scnb.coupon.calculation.controller.service.intf.CouponCalculationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("calculator")
@Slf4j
public class CouponCalculationController {
    
    @Autowired
    private CouponCalculationService couponCalculationService;
    
    // 优惠券结算
    @PostMapping("/checkout")
    @ResponseBody   // 将java对象转换为json格式的数据
    public ShoppingCart calculateOrderPrice(@RequestBody ShoppingCart settlement) {
        log.info("do calculation:{}", JSON.toJSONString(settlement));
        return couponCalculationService.calculateOrderPrice(settlement);
    }   
    
    // 优惠券列表挨个试算
    // 给客户提示每个可用券的优惠额度，帮助挑选
    @PostMapping("/simulate")
    @ResponseBody
    public SimulationResponse simulate(@RequestBody SimulationOrder order) {
        return couponCalculationService.simulateOrder(order);
    }
    
}
